package com.joe.xl.common.framework.controller;


import com.github.pagehelper.PageInfo;
import com.joe.xl.common.framework.service.ICrmCustomerService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author joe
 * @since 2020-08-21
 */
@RestController
@RequestMapping("/test")
public class CrmCustomerController {
    private final static Logger logger = LoggerFactory.getLogger(CrmCustomerController.class);

    @Autowired
    private ICrmCustomerService customerService;

    @RequestMapping("/testmaster")
    public Map<String, Object> testMaster(){
        logger.info("into testmaster ...");

        Map<String, Object> map = customerService.testMaster();

        return map;
    }

    @HystrixCommand(fallbackMethod = "defaultHys", ignoreExceptions = {Exception.class},
            commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    @RequestMapping("/hysmaster")
    public Map<String, Object> hysmaster(){
        logger.info("into hysmaster ...");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
//            e.printStackTrace();
        }

        Map<String, Object> map = customerService.testMaster();

        return map;
    }

    @RequestMapping("/testmaster1")
    public Map<String, Object> testMaster1(){
        Map<String, Object> map = customerService.testMaster1();

        return map;
    }

    @RequestMapping("/testmaster2")
    public Map<String, Object> testMaster2(){
        Map<String, Object> map = customerService.testMaster2();

        return map;
    }

    @RequestMapping("/testPagehelper")
    public PageInfo<Map<String, Object>> testPagehelper(int pageNo, int pageSize){
        pageNo = pageNo == 0 ? 1 : pageNo;
        pageSize = pageSize == 0 ? 10 : pageSize;

        PageInfo<Map<String, Object>> pageInfo = customerService.testPagehelper(pageNo, pageSize);

        return pageInfo;
    }

    private Map<String, Object> defaultHys(){
        Map<String, Object> map = new HashMap<>();
        map.put("code", "-1");
        map.put("msg", "timeout..");

        return map;
    }
}
